import { fromJS, } from "immutable";
import * as constants from './constants'
const defaultState = fromJS({
  FirstFilter:[{
    id:1,
    content: '智能排序',
    show: false,
    condition:0,
    query: 'intelligence'
  },
    {
      id: 2,
      content: '时间排序',
      show: false,
      condition:1,
      query: 'time'
    }],
  SecondFilter:[{
    id: 3,
    content: '标题',
    show: false,
    condition:0,
    query: 'title',
  },{
    id: 4,
    content: '正文',
    show: false,
    condition:0,
    query: 'content'
  },{
    id: 5,
    content: '标题或正文',
    show: false,
    condition:1,
    query: 'all'
  }],
  ThirdFilter:[{
    id: 6,
    content: '全部时间',
    show: false,
    condition:1,
    query: 'all'
  },
    {
      id: 7,
      content: '一天以内',
      show: false,
      condition:0,
      query: 'day'
    },
    {
      id: 8,
      content: '一周以内',
      show: false,
      condition:0,
      query: 'week'
    },{
      id: 9,
      content: '一月以内',
      show: false,
      condition:0,
      query: 'month',
    },{
      id: 10,
      content: '一年以内',
      show: false,
      condition:0,
      query: 'year'
    }],
  data: [],
  item: [{key:"无",num:1}],
  InputValue: '',
  totalPage: 1,
  currentPage: 1,
  pageSize: 5,
  groupCount: 7,
  startPage:1,
  answer: '无'
})

export default ( state = defaultState, action ) => {
  switch(action.type){
    case constants.CHANGE_SHOW_STATE:
      const array = state.get(action.data.name).toJS();
      for(let i = 0; i < array.length;i++){
        if(array[i].id===action.data.index){
          array[i].show = !array[i].show;
        }
      }
      return state.set(action.data.name,fromJS(array))
    case constants.CHANGE_CURRENT_PAGE:
      return state.set('currentPage',action.data)
    case constants.CHANGE_START_PAGE:
      return state.set('startPage',action.data)
    case constants.SHOW_SEARCH_KEYWORD:
      return state.set('item',fromJS(action.data))
    case constants.CHANGE_INPUT_VALUE:
      return state.set('InputValue',action.data)
    case constants.CHANGE_CONDITION_STATE:
      const arr = state.get(action.data.name).toJS();
      for(let i = 0; i < arr.length;i++){
        if(arr[i].id===action.data.index){
          arr[i].condition = 1;
          continue;
        }
        arr[i].condition = 0;
      }
      return state.set(action.data.name,fromJS(arr))
    case constants.ChANGE_SEARCH_CONTENT:
      const content = [];
      for(let i =0;i<action.data.result.length;i++){
        let {_id,material,qualifications,title,date } = action.data.result[i];
        title = title.split(".")[0]
        let c = "";
        for (let j =0;j<material.length;j++) c += material[j];
        for (let j =0;j<qualifications.length;j++) c += qualifications[i];
        content.push({id:_id,title:title,date:date,content: c});
      }
      return state.merge({'data':fromJS(content),'totalPage':Math.ceil(parseInt(action.data.total)/ state.get('pageSize')),'currentPage':1,'startPage':1})
    case constants.CHANGE_PAGE_CONTENT:
      const cont = []
      for(let i =0;i<action.data.result.length;i++){
        let {_id,material,qualifications,title,date } = action.data.result[i];
        title = title.split(".")[0]
        let c = "";
        for (let j =0;j<material.length;j++) c += material[j];
        for (let j =0;j<qualifications.length;j++) c += qualifications[i];
        cont.push({id:_id,title:title,date:date,content: c});
      }
      return state.merge({'data':fromJS(cont),'totalPage':Math.ceil(parseInt(action.data.total)/ state.get('pageSize'))})
    case constants.SET_SHOW_ANSWER:
      const { message } = action.data;
      if(message==="success"&&action.data.data.type===5000){
         return state.set('answer',action.data.data.info.text)
      }
    default:
      return state
  }
}
